﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.Categories.Service
{
    public class CategoryAppService : BaseAppService, ICategoryAppService
    {
        private ICategoryDomainService _domainService;
        private ICategoryVerifyDataManager _categoryVerifyDataManager;

        public CategoryAppService(ICategoryDomainService domainService,
            ICategoryVerifyDataManager categoryVerifyDataManager)
            : base()
        {
            _domainService = domainService;
            _categoryVerifyDataManager = categoryVerifyDataManager;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.CATEGORY, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyDelete(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.CATEGORY, AuditAction.CREATE)]
        public Dto.CategoryDto Add(Guid botId, CategoryCreateDto dto)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyCreate(botId,dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            var category = Mapper.Map<Category>(dto);
            category.BotId = botId;
            category = _domainService.Create(category);          

            return Mapper.Map<Dto.CategoryDto>(category);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.CATEGORY, AuditAction.UPDATE)]
        public Dto.CategoryDto Update(Guid id, CategoryUpdateDto dto)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyUpdate(id, dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            var categoryUpdateBo = Mapper.Map<CategoryUpdateBo>(dto);
            categoryUpdateBo.Id = id;
            Category category = _domainService.Update(id, categoryUpdateBo);

            return Mapper.Map<Dto.CategoryDto>(category);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.CATEGORY, AuditAction.DESTROY)]
        public void Reassign(Guid id, Guid targetCategoryId)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyReassign(id, targetCategoryId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            _domainService.Reassign(id, targetCategoryId);
        }

        public Dto.CategoryDto Get(Guid id)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyGet(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            Category category = _domainService.Get(id);
            return Mapper.Map<Dto.CategoryDto>(category);
        }

        public IEnumerable<Dto.CategoryDto> GetList()
        {
            IEnumerable<Category> list = _domainService.List();
            return Mapper.Map<IEnumerable<Dto.CategoryDto>>(list);
        }
        public List<Dto.CategoryDto> GetByBotId(Guid botId)
        {
            var errorInfo = _categoryVerifyDataManager.VerifyGetList(botId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            var list = _domainService.GetByBotId(botId);
            return Mapper.Map<List<Dto.CategoryDto>>(list);
        }
    }
}
